package com.tarena.lbs.user.service.impl;


import com.tarena.lbs.base.common.utils.Asserts;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.common.encoder.JwtEncoder;
import com.tarena.lbs.common.enums.PassportResultEnum;
import com.tarena.lbs.common.enums.Roles;
import com.tarena.lbs.common.principle.UserPrinciple;
import com.tarena.lbs.common.utils.AuthenticationContextUtils;
import com.tarena.lbs.user.GlobalExceptionHandle.ServiceException;
import com.tarena.lbs.user.dao.repository.AdminRepository;
import com.tarena.lbs.user.dao.repository.UserRepository;
import com.tarena.lbs.user.pojo.param.AdminLoginParam;
import com.tarena.lbs.user.pojo.param.UserLoginParam;
import com.tarena.lbs.user.pojo.po.AdminPO;
import com.tarena.lbs.user.pojo.po.UserPO;
import com.tarena.lbs.user.pojo.vo.LoginVO;
import com.tarena.lbs.user.service.LoginService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;



@Slf4j
@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    AdminRepository adminRepository;
    @Autowired
    UserRepository userRepository;
    @Autowired
    private JwtEncoder<UserPrinciple> jwtEncoder;

    @Override
    public LoginVO adminLogin(AdminLoginParam param) throws BusinessException {
        String phone = param.getPhone();
        String password = param.getPassword();
        //如果手机号存在
        List<AdminPO> adminPOList = adminRepository.getPasswordByPhone(phone);
        Asserts.isTrue(adminPOList.size() <= 0,PassportResultEnum.USER_NONE_EXIST);
        AdminPO adminPO = adminPOList.get(0);
        Asserts.isTrue(!adminPO.getAccountPassword().equals(password),PassportResultEnum.USER_PASSWORD_ERROR);
        LoginVO loginVO = new LoginVO();
        loginVO.setJwt(generateJwt(adminPO));
        loginVO.setId(adminPO.getId());
        loginVO.setNickname(adminPO.getNickname());
        return loginVO;
        }

    @Override
    public LoginVO userLogin(UserLoginParam param) throws BusinessException {
        String phone = param.getPhone();
        String password = param.getPassword();
        List<UserPO> userPOList = userRepository.getPasswordByPhone(phone);
        if (userPOList.size() > 0) {
            UserPO userPO = userPOList.get(0);
            if (userPO.getPassword().equals(password)) {
                LoginVO loginVO = new LoginVO();
                loginVO.setJwt(generateUserJwt(userPO));
                loginVO.setId(userPO.getId());
                loginVO.setNickname(userPO.getNickName());
                return loginVO;
            }else{
                throw new ServiceException(PassportResultEnum.USER_PASSWORD_ERROR);
            }
        }
        throw new ServiceException(PassportResultEnum.USER_NONE_EXIST);




    }

    private String generateJwt(AdminPO po) throws BusinessException {
        //在jwt中携带的用户信息 并不是adminPO 而是单独设计的一个 用户认证对象 UserPrinciple
        //原因是要和数据库表格解耦
        UserPrinciple userPrinciple = new UserPrinciple();
        userPrinciple.setId(po.getId());
        userPrinciple.setNickname(po.getNickname());
        //角色 在后台管理中 有2个角色 admin表格 type=0 平台 type=1 商家
        //在认证对象放的角色是一个枚举Role ADMIN SHOP USER
        userPrinciple.setRole(po.getAccountType()==0? Roles.ADMIN:Roles.SHOP);
        AuthenticationContextUtils.save(userPrinciple);
        //jwtEncoder 生成jwt的工具类
        return jwtEncoder.generateToken(userPrinciple);
    }

    private String generateUserJwt(UserPO po) throws BusinessException {
        //在jwt中携带的用户信息 并不是adminPO 而是单独设计的一个 用户认证对象 UserPrinciple
        //原因是要和数据库表格解耦
        UserPrinciple userPrinciple = new UserPrinciple();
        userPrinciple.setId(po.getId());
        userPrinciple.setNickname(po.getNickName());
        //在认证对象放的角色是一个枚举Role ADMIN SHOP USER
        userPrinciple.setRole(Roles.USER);
        AuthenticationContextUtils.save(userPrinciple);
        //jwtEncoder 生成jwt的工具类
        return jwtEncoder.generateToken(userPrinciple);
    }
}













